<template>
  <div>
    <home-header></home-header>
    <home-swiper></home-swiper>
    <home-icons></home-icons>
    <home-recommend :recommend-list="recommendList"></home-recommend>
    <home-weekend></home-weekend>
  </div>
</template>

<script>
import HomeHeader from "./components/HomeHeader";
import HomeSwiper from "./components/HomeSwiper";
import HomeIcons from "./components/HomeIcons";
import HomeRecommend from "./components/HomeRecommend"
import HomeWeekend from "./components/HomeWeekend"
import axios from "axios/index"
import { mapState } from "vuex"

export default {
  name: "home",
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  data() {
    return {
      recommendList: [],
      lastCity: ""
    }
  },
  methods: {
    handleIndexData(data) {
      console.log(data);
      console.log(data.data);
      this.recommendList = data.data.recommendList;
    },
    getIndexData() {
      axios.get("/api/index.json?city=" + this.city)
        .then((response) => {
          this.handleIndexData(response.data);
        })
    }
  },
  mounted() {
    this.lastCity = this.city;
    this.getIndexData();
  },
  activated() {
    if (this.lastCity !== this.city) {
      this.getIndexData();
      this.lastCity = this.city;
    }
  },
  computed: {
    ...mapState(["city"])
  }
};
</script>
